import xml.dom.minidom
import tkinter as tk
from tkinter import ttk
import socket
import json

#open xml file
dom = xml.dom.minidom.parse('UNData.xml')

#get elements in the file
root = dom.documentElement

#search by Tag
cc = dom.getElementsByTagName('Country')
country = []
for c in cc:
    # elements of country
    
    a = 0
    while a < len(cc):
        country.append(cc[a].firstChild.data)
        a+=28

yy = dom.getElementsByTagName('Year')
year = []
for y in yy:
    # elements of year
    
    b = 0
    while b < len(yy):
        year.append(yy[b].firstChild.data)
        b+=1

vv = dom.getElementsByTagName('Value')
value = []
for v in vv:
    # elements of value
    
    d = 0
    while d < len(vv):
        value.append(vv[d].firstChild.data)
        d+=1

print(len(country),len(year))
# use Tkinter , create a window
# win = tk.Tk()
# win.title("Select a country") # Title of the window
# win.geometry("500x300+200+20")

# '''
# pull-down menu
# '''
# xVariable = tk.StringVar()

# # Create a pull-down menu
# com = ttk.Combobox(win, textvariable=xVariable)
# com.pack()
# com["value"] = country
# com.current(0) # Set a default value

# com.bind("<<ComboboxSelected>>")
# country_select = com.get()
# p = country.index(country_select)
# win.mainloop()
# '''
# Server
# '''
# ip_port = (socket.gethostname(), 5000)
# sk = socket.socket()            # create a socket

# sk.bind(ip_port)                # bind host address and port together
# sk.listen(5)                    # # configure how many client the server can listen simultaneously
# print('Starting socket，waiting for connection...')
# conn, address = sk.accept()     # # accept new connection

# while True:
#     # receive data stream. it won't accept data packet greater than 1024 bytes
#     client_data = conn.recv(1024).decode()
#     y_range = year[0:28]
#     dic = dict(zip(y_range,value[(0 + p * 28):(28 + p * 28)]))
#     dataStr = json.dumps(dic)

#     if client_data == "exit":
#         exit("You have exit.")

#     print("Message from Client %s：%s" % (address, client_data))
#     conn.send(dataStr.encode())   # # send data to the client
#     conn.close() # close the connection